﻿using System.Data;
using System.Reflection;

namespace Utils
{
    public class JsonHelper
    {
        /// <summary>
        /// 对象转Json
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string ObjectToJson(object obj)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
        }

        /// <summary>
        /// 解析JSON字符串生成对象实体
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
        /// <returns>对象实体</returns>
        public static T? DeserializeJsonToObject<T>(string json) where T : class
        {
            try
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
            }
            catch (Exception ex) { }
            {
                return null;
            }
        }

        /// <summary>
        /// 对象转成另一对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T? DeserializeObjectToObject<T>(object obj) where T : class
        {
            string json = ObjectToJson(obj);
            return DeserializeJsonToObject<T>(json);
        }

        /// <summary>
        /// 将DataRow转换成实体对象
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="dr">数据行</param>
        /// <returns>实体类</returns>
        public static T DataRowToModel<T>(DataRow dr)
        {
            Type temp = typeof(T);
            T obj = Activator.CreateInstance<T>();
            foreach (DataColumn column in dr.Table.Columns)
            {
                foreach (PropertyInfo pro in temp.GetProperties())
                {
                    if (pro.Name.ToLower() == column.ColumnName.ToLower())
                    {
                        if (dr[column.ColumnName] == DBNull.Value)
                        {
                            if (column.DataType.Name == "DateTime")
                                pro.SetValue(obj, new DateTime(), null);
                            else
                                pro.SetValue(obj, " ", null);
                            break;
                        }
                        else
                        {

                            pro.SetValue(obj, dr[column.ColumnName], null);
                            break;

                        }
                    }
                }
            }
            return obj;
        }

        /// <summary>
        /// 如果类型可空，则返回基础类型，否则返回类型
        /// </summary>
        private static Type? GetCoreType(Type t)
        {
            if (t != null && IsNullable(t))
            {
                if (!t.IsValueType)
                {
                    return t;
                }
                else
                {
                    return Nullable.GetUnderlyingType(t);
                }
            }
            else
            {
                return t;
            }
        }

        /// <summary>
        /// 指定类型是否可为空
        /// </summary>
        private static bool IsNullable(Type t)
        {
            return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
        }
    }
}